package com.wx.web.controller.token;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.eachonline.core.JsonResult;
import com.eachonline.util.RedisHelper;
import com.wx.business.TicketSign;
import com.wx.business.WXConfig;
import com.wx.web.common.WXHttpUtil;



@Controller
@RequestMapping("/token")
public class WxTokenController  {
	
	
	@RequestMapping(value = "/getToken", method = { RequestMethod.POST })
	@ResponseBody
	public JsonResult userGetToken(HttpServletRequest  request ,HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        
        String accessToken= RedisHelper.get("access_token");
        if(StringUtils.isEmpty(accessToken))
        {
        	 String tokenJson=WXHttpUtil.requestAccessToken();
             JSONObject object=JSONObject.parseObject(tokenJson);
             if(object.get("access_token")==null||object.get("access_token")=="")
             {
             	
             	 return  new JsonResult(true,"获取token失败");
             }
             accessToken=object.get("access_token")+"";
             RedisHelper.set("access_token",  accessToken);;
        }
        Map<String,Object> dataMap=new HashMap<String,Object>();

        dataMap.put("access_token", accessToken);
        
        return  new JsonResult(true,dataMap);
       
	}
	

	@RequestMapping(value = "/getCodeToken", method = { RequestMethod.GET })
	@ResponseBody
	public void userGetToken(HttpServletRequest  request ,HttpServletResponse response,String code) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        
        String tokenJson=WXHttpUtil.requestCodeToken(code);
        JSONObject object=JSONObject.parseObject(tokenJson);
        
        Map<String,Object> dataMap=new HashMap<String,Object>();
        if(object.get("access_token")==null||object.get("access_token")=="")
        {
        	// return  new JsonResult(true,"获取token失败");
        }
        
        dataMap.put("access_token", object.get("access_token"));
        dataMap.put("expires_in", object.get("expires_in"));
        dataMap.put("refresh_token", object.get("refresh_token"));
        dataMap.put("openid", object.get("openid"));
        dataMap.put("scope", object.get("scope"));
        
        response.sendRedirect("/wxuser/getInfo?openid="+object.get("openid")+"&accessToken="+object.get("access_token"));
	}

	
	@RequestMapping(value = "/getTicket", method = { RequestMethod.POST })
	@ResponseBody
	public JsonResult geTicket(HttpServletRequest  request ,HttpServletResponse response,String accessToken) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        String tokenJson=WXHttpUtil.requestTicket(accessToken);
        JSONObject object=JSONObject.parseObject(tokenJson);
        
        Map<String,Object> dataMap=new HashMap<String,Object>();
        if(object.get("ticket")==null||object.get("ticket")=="")
        {
        	
        	 return  new JsonResult(true,"获取ticket失败");
        }
        dataMap.put("ticket", object.get("ticket"));
        
        return  new JsonResult(true,dataMap);
       
	}

	
	/**
	 * 分享签名验证
	 * @param request
	 * @param response
	 * @param url
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getTicketSign")
	@ResponseBody
	public JsonResult getTicketSign(HttpServletRequest  request ,HttpServletResponse response,String url) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        
        String accessToken="";
        if(StringUtils.isEmpty(accessToken))
        {
        String tokenJson=WXHttpUtil.requestAccessToken();
        JSONObject object=JSONObject.parseObject(tokenJson);
        
        if(object.get("access_token")==null||object.get("access_token")=="")
        return  new JsonResult(true,"获取token失败");
        
         accessToken=object.get("access_token")+"";
         RedisHelper.set("access_token", accessToken);
        }
        
        String ticket= "";
        if(StringUtils.isEmpty(ticket))
        {
        	 String  strticket=WXHttpUtil.requestTicket(accessToken);
             JSONObject ticketObject=JSONObject.parseObject(strticket);
             
             if(ticketObject.get("ticket")==null||ticketObject.get("ticket")=="")
             return  new JsonResult(true,"获取ticket失败");
             
             ticket=ticketObject.get("ticket")+"";
             RedisHelper.set("ticket",  ticket);
        }
       
        
       Map<String,String> dataMap= TicketSign.sign(ticket, url);
       dataMap.put("appId", WXConfig.AppID);
        return  new JsonResult(true,dataMap);
	}
}
